package com.ruoyi.web.controller.hh;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.HhBrand;
import com.ruoyi.domain.HhType;
import com.ruoyi.service.IHhBrandService;
import com.ruoyi.service.IHhTypeService;
import com.ruoyi.utils.SelectHelpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 商品品牌控制器
 * @author chongjin
 * @date 2020-12-17
 */
@Controller
public class HhBrandController extends BaseController {
    private String prefix = "hh/baseInfo";

    @Autowired
    private IHhBrandService hhBrandService;
    @Autowired
    private IHhTypeService hhTypeService;

    /**
     * 获取品牌管理页面
     */
    @GetMapping("/baseInfo/brand")
    public String brand(ModelMap mmap) {
        HhType hhType = new HhType();
        hhType.setParentId(Long.parseLong("0"));
        List typeList = hhTypeService.selectHhTypeList(hhType);
        mmap.put("parentType", SelectHelpUtil.getNameType(typeList));
        return prefix + "/brand_manage";
    }

    /**
     * 查询商品品牌列表
     */
    @PostMapping("/baseInfo/brand/list")
    @ResponseBody
    public TableDataInfo list(HhBrand hhBrand) {
        startPage();
        List<HhBrand> list = hhBrandService.selectHhBrandList(hhBrand);
        return getDataTable(list);
    }

    /**
     * 查询商品类型
     */
    @PostMapping("/baseInfo/brand/getBrand")
    @ResponseBody
    public AjaxResult getType(HhBrand hhBrand) {
        List<HhBrand> list = hhBrandService.selectHhBrandList(hhBrand);
        return AjaxResult.success(SelectHelpUtil.getNameBrand(list));
    }

    /**
     * 获取新增品牌页面
     */
    @GetMapping("/baseInfo/brand/add")
    public String add(ModelMap mmap) {
        HhType hhType = new HhType();
        hhType.setParentId(Long.parseLong("0"));
        List typeList = hhTypeService.selectHhTypeList(hhType);
        mmap.put("parentType", SelectHelpUtil.getNameType(typeList));
        return prefix + "/brand_add";
    }

    /**
     * 新增保存商品品牌
     */
    @Log(title = "新增品牌", businessType = BusinessType.INSERT)
    @PostMapping("/baseInfo/brand/add")
    @ResponseBody
    public AjaxResult addSave(HhBrand hhBrand) {
        return toAjax(hhBrandService.insertHhBrand(hhBrand));
    }

    /**
     * 获取修改品牌页面
     */
    @GetMapping("/baseInfo/brand/edit/{brandId}")
    public String edit(@PathVariable("brandId") Long brandId, ModelMap mmap) {
        HhBrand hhBrand = hhBrandService.selectHhBrandById(brandId);
        mmap.put("hhBrand", hhBrand);
        return prefix + "/brand_edit";
    }

    /**
     * 修改保存商品品牌
     */
    @Log(title = "修改品牌", businessType = BusinessType.UPDATE)
    @PostMapping("/baseInfo/brand/edit")
    @ResponseBody
    public AjaxResult editSave(HhBrand hhBrand) {
        return toAjax(hhBrandService.updateHhBrand(hhBrand));
    }

    /**
     * 删除商品品牌
     */
    @Log(title = "删除品牌", businessType = BusinessType.DELETE)
    @PostMapping("/baseInfo/brand/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(hhBrandService.deleteHhBrandByIds(ids));
    }
}
